<?php

/**
 * 微信端文件夹相关操作
*/
class WfloderController extends OAuth2Controller {

    protected $app_type = '';

    /**
     * 根据传参判断应用类型
    */
    public function init() {
        $app_type = Yii::app()->request->getParam('type');
        if (empty($app_type) || !in_array($app_type, array('image','video'))) {
            $this->error('非法的应用类型');
        }
        $this->app_type = $app_type;
        parent::init();
    }

    /**
     * 列出目录列表
    */
    public function actionList() {
        $type = $this->app_type;
        if (empty($type)) {
            $this->error('参数非法');
        }
        $model = Floder::model();
        $model->type = $type;
        if (Yii::app()->request->isAjaxRequest) {
            $pid = Yii::app()->request->getParam('pid');
            $page = Yii::app()->request->getParam('page', 0);
            $model->pid = $pid;
            $page_list = $model->getPageList($page);
            $default_thumb = ''; //默认缩略图
            foreach ($page_list['data'] as $k => $v) {
                if (empty($v['thumb_src'])) {
                    if (empty($default_thumb)) {
                        foreach ($page_list['data'] as $k1 => $v1) {
                            if (!empty($v1['thumb_src'])) {
                                $default_thumb = $v1['thumb_src'];
                                break;
                            }
                        }
                    }
                    $v['thumb_src'] = $default_thumb;
                }
                $v['url'] = $this->createUrl("w{$v['type']}/list", array('pid' => $v['id']));
                $page_list['data'][$k] = $v;
            }
            //获取标题，存在pid就获取目录名称，不存在就存目录类别
//            $title = empty($pid) ? Floder::$type_map[$type] : Floder::model()->findByPk($pid)->name;
//            $page_list = array_merge($page_list, array('title'=>$title));
            $this->jsonResponse($page_list);
        } else {
            $parent_floder = $model->getParentFloder();
            $params = array(
                'parent_floder' => $parent_floder,
                'default_fid' => isset($parent_floder[0]) ? $parent_floder[0]->id : 0,
                'type' => $type
            );
            $this->render('list', $params);
        }
    }
}